//
//  MemberTableView.m
//  Cat
//
//  Created by lushitong on 2018/12/1.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "MemberTableView.h"
#import "MemberTopTableViewCell.h"
#import "HttpUserManager.h"

@interface MemberTableView()

@property (nonatomic, strong) MemberTopTableViewCell *topCell;

@end

@implementation MemberTableView

- (instancetype)initWithFrame:(CGRect)frame sections:(NSInteger)sections {
	if (self = [super initWithFrame:frame sections:sections]) {
		self.addRefresh = NO;
		self.backgroundColor = [UIColor whiteColor];
		self.tableView.backgroundColor = [UIColor whiteColor];
		self.tableView.showsVerticalScrollIndicator = NO;
		self.noFirstAutoRequest = NO;
		self.outerControlLoading = NO;
	}
	return self;
}

- (void)sendRequest {
	[HttpUserManager getMemberInfoSucces:^(MemberModel * _Nonnull member) {
		self.model = member;
		[self p_setupMemberTopCell];
		[self p_setupMemberCell];
		[self finishLoadData:@[]];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
	CGFloat minOffset = - 200.0;
	CGFloat offsetY = scrollView.contentOffset.y + StatusBarHeight;
	if (offsetY < minOffset) {
		scrollView.contentOffset = CGPointMake(0, minOffset - StatusBarHeight);
	} else if (offsetY < 0 && offsetY >= minOffset) {
		[self.topCell updateCoverWithOffset:offsetY];
	} else {
		[self.topCell updateCoverWithOffset:0];
	}
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat cellHeight  = [super tableView:tableView heightForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		cellHeight = kQKFitModule(80 + 134) + StatusBarSafeInsetHeight;
	} else if (indexPath.section == 1) {
		cellHeight = kQKFitModule(79 + 64);
	}
	return cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *sectionCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		MemberTopTableViewCell *cell = (MemberTopTableViewCell *)sectionCell;
		self.topCell = cell;
	}
	return sectionCell;
}

- (void)p_setupMemberTopCell {
	QKBaseTableCellModel *memberCellData = [[QKBaseTableCellModel alloc] init];
	memberCellData.isDynamic = NO;
	memberCellData.cellClass = @"MemberTopTableViewCell";
	memberCellData.section = 0;
	memberCellData.cellData = self.model;
	memberCellData.cellHeight = kQKFitModule(80 + 134) + StatusBarSafeInsetHeight;
	[self addTableSectionData:memberCellData];
}

- (void)p_setupMemberCell {
	QKBaseTableCellModel *infoCellData = [[QKBaseTableCellModel alloc] init];
	infoCellData.isDynamic = NO;
	infoCellData.cellClass = @"MemberInfoTableViewCell";
	infoCellData.section = 1;
	infoCellData.cellData = self.model;
	infoCellData.cellHeight = kQKFitModule(79 + 64);
	[self addTableSectionData:infoCellData];
}

#pragma mark----- lazy init -----

- (MemberTopTableViewCell *)topCell {
	if (!_topCell) {
		_topCell = [[MemberTopTableViewCell alloc] init];
	}
	return _topCell;
}


@end
